package com.beagle.njddapp.weight.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.beagle.njddapp.R;
import com.blankj.utilcode.util.ToastUtils;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author hxx
 * @date 2021-03-13 20:13
 *
 */
public class MapDialog extends Dialog {


    private  double lat,lnt;
    private String address;

    public MapDialog(@NonNull Context context) {
        super(context);
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public void setLnt(double lnt) {
        this.lnt = lnt;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_choose_map);
        ButterKnife.bind(this);
        if (getWindow() != null) {
            //按空白处不能取消动画
            setCanceledOnTouchOutside(true);
            //设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            //一定要在setContentView之后调用，否则无效
            getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            getWindow().setWindowAnimations(R.style.DialogAnim);

        }
    }
    @OnClick({R.id.tv_gd,R.id.tv_bd,R.id.tv_tx,R.id.tv_close,R.id.container})
    public void onCLick(View view){
        switch (view.getId()){
            case R.id.container:
                dismiss();
                break;
            case R.id.tv_gd:
                openGaoDeMap(lat,lnt,address);
                dismiss();
                break;
            case R.id.tv_bd:
                openBaiduMap(lat,lnt,address);
                dismiss();
                break;
            case R.id.tv_tx:
                openTxMap(lat,lnt,address);
                dismiss();
                break;
            case R.id.tv_close:
                dismiss();
                break;
        }
    }

    private void openGaoDeMap(double dlat, double dlon, String dname) {
        if (checkMapAppsIsExist(getContext(), "com.autonavi.minimap")) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setPackage("com.autonavi.minimap");
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setData(Uri.parse("androidamap://route?sourceApplication=" + R.string.app_name
                    + "&sname=我的位置&dlat=" + dlat
                    + "&dlon=" + dlon
                    + "&dname=" + dname
                    + "&dev=0&m=0&t=0"));
            getContext().startActivity(intent);
        } else {
            ToastUtils.showShort( "高德地图未安装");
        }
    }

    private void openBaiduMap(double dlat, double dlon, String dname) {
        if (checkMapAppsIsExist(getContext(), "com.baidu.BaiduMap")) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("baidumap://map/direction?origin=我的位置&destination=name:"
                    + dname
                    + "|latlng:" + dlat + "," + dlon
                    + "&mode=driving&sy=3&index=0&target=1"));
            getContext().startActivity(intent);
        } else {
            ToastUtils.showShort( "百度地图未安装");
        }
    }
    private void openTxMap(double dlat, double dlon, String dname) {
        if (checkMapAppsIsExist(getContext(), "com.tencent.map")) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("qqmap://map/routeplan?type=drive&from=我的位置&fromcoord=0,0"
                    + "&to=" + dname
                    + "&tocoord=" + dlat + "," + dlon
                    + "&policy=1&referer=myapp"));
            getContext().startActivity(intent);
        } else {
            ToastUtils.showShort( "腾讯地图未安装");
        }
    }

    public boolean checkMapAppsIsExist(Context context, String packagename) {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(packagename, 0);
        } catch (Exception e) {
            packageInfo = null;
            e.printStackTrace();
        }
        return packageInfo != null;
    }

}
